﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ExtenDev.Web.Extensions;

namespace ExtenDev.Web
{
    /// <summary>
    /// Automatically registers and facilitates all necessary extensions for an application using the MicroAjax framework.
    /// It is not mandatory that an application inherit from this class or its master/page/control counterparts, but one that 
    /// does will require little to no additional setup to use the entire MicroAjax framework.
    /// </summary>
    public class MicroApplication : HttpApplication
    {
        private ControlMethodImpl controlMethodProcessor;

        public override void Init()
        {
            base.Init();

            controlMethodProcessor = CreateControlMethodProcessor();

            base.PostMapRequestHandler += new EventHandler(MicroApplication_PostMapRequestHandler);
            base.PostAcquireRequestState += new EventHandler(MicroApplication_PostAcquireRequestState);
        }

        protected virtual ControlMethodImpl CreateControlMethodProcessor() {
            return new ControlMethodImpl();
        }

        void MicroApplication_PostAcquireRequestState(object sender, EventArgs e)
        {
            controlMethodProcessor.ApplySessionRequestHandler(this);
        }

        void MicroApplication_PostMapRequestHandler(object sender, EventArgs e)
        {
            controlMethodProcessor.ProcessRestRequest(this);
        }
    }
}
